﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Web.Mvc;
using www.Models;
//using www.SService;

namespace www.Controllers
{
    public class StoryControllers : Controller
    {
        //private static ObjectCache _cache = MemoryCache.Default;
        //private readonly ServiceClient _service1 = new ServiceClient();
        //private readonly Utility _utility = new Utility();
        ////
        //// GET: /Story/
        //[HttpGet]
        //public ActionResult Index()
        //{
        //    Type();
        //    Category();
        //    var listItemsList = new List<SelectListItem>();
        //     var lType = (List<RelaxTypeDataContract>)_cache["Type"];
            
        //     listItemsList.AddRange(lType.Select(item => new SelectListItem { Text = item.Name, Value = item.Id.ToString() }));
        //    ViewBag.Types = listItemsList;

        //    var listCategory = new List<SelectListItem>();
        //    var lCategory = (List<RelaxCategoryDataContract>)_cache["Category"];
        //    listCategory.AddRange(lCategory.Where(p =>
        //    {
        //        var relaxTypeDataContract = lType.FirstOrDefault();
        //        return relaxTypeDataContract != null ;
        //    }).Select(item => new SelectListItem { Text = item.Name, Value = item.Id.ToString() }));
        //    ViewBag.Category = listCategory;

        //    return View();
        //}

        //private void Type()
        //{
        //    if (_cache["Type"] == null)
        //    {
        //        var lType = _service1.GeTypeDataContracts();
        //        if (lType.Any())
        //        {
        //            var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(27) };
        //            _cache.Set("Type", lType, policy);

        //        }
        //    }
           
        //}
        //private void LoadLinkType(string target = null)
        //{
        //    if (Session["LinkType"] == null)
        //    {
        //        var linkType = new List<SelectListItem>
        //        {
        //            new SelectListItem {Text = "_self", Value = "_self",Selected = target=="_self"},
        //            new SelectListItem {Text = "_blank", Value = "_blank",Selected = target=="_blank"},
        //            new SelectListItem {Text = "_parent", Value = "_parent",Selected = target=="_parent"},
        //            new SelectListItem {Text = "_top", Value = "_top",Selected = target=="_top"}
        //        };
        //        Session["LinkType"] = linkType;
        //    }
           
        //}
        //private void Category()
        //{
        //    if (_cache["Category"] == null)
        //    {
        //        var lCategory = _service1.GetCategoryDataContracts();
        //        if (lCategory.Any())
        //        {
        //            var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(27) };
        //            _cache.Set("Category", lCategory, policy);
        //        }
        //    }
        //}
        //[HttpPost]
        //public ActionResult LoadCategory(FormCollection collection)
        //{
        //    Category();

        //    return RedirectToAction("Index", "Story");
        //}
        //[HttpPost]
        //public ActionResult Index(FormCollection collection)
        //{
           
        //    var listItemsList = new List<SelectListItem>();
        //    var lType = (List<RelaxTypeDataContract>)_cache["Type"];

        //    listItemsList.AddRange(lType.Select(item => new SelectListItem { Text = item.Name, Value = item.Id.ToString(),Selected = item.Id==Convert.ToInt32(collection["Types"])}));
        //    ViewBag.Types = listItemsList;

        //    var listCategory = new List<SelectListItem>();
        //    var lCategory = (List<RelaxCategoryDataContract>)_cache["Category"];
        //    listCategory.AddRange(lCategory.Select(item => new SelectListItem { Text = item.Name, Value = item.Id.ToString(), Selected = item.Id == Convert.ToInt32(collection["Category"]) }));
        //    ViewBag.Category = listCategory;

        //    if (_cache["Story"] == null)
        //    {
        //        var lStory = _service1.GetRelaxStoryDataContracts();
        //        if (lStory.Any())
        //        {
        //            var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(27) };
        //            _cache.Set("Story", lStory, policy);
        //            var lData = lStory.Where(p => p.CategoryId == Convert.ToInt32(collection["Category"]));
        //            ViewData["Data"] = lData;
        //        }
        //    }
        //    else
        //    {
        //        var lStory = (List<RelaxStoryDataContract>)_cache["Story"];
        //        var lData = lStory.Where(p => p.CategoryId == Convert.ToInt32(collection["Category"]));
        //        ViewData["Data"] = lData;

        //    }
        //    return View();
        //}

        //[HttpPost]
        //public JsonResult LoadType(int? type)
        //{
        //    //var listItemsList = new List<SelectListItem>();
        //    //var lType = (List<RelaxTypeDataContract>)_cache["Type"];
        //    //listItemsList.AddRange(lType.Select(item => new SelectListItem { Text = item.Name, Value = item.Id.ToString(), Selected = item.Id == type }));
        //    //ViewBag.Type = listItemsList;
        //    var categoryType = new CategoryTypeModels();
        //    var lType = (List<RelaxTypeDataContract>)_cache["Type"];
        //    categoryType.RelaxType = lType;
        //    var lStory = (List<RelaxCategoryDataContract>)_cache["Category"];
        //    if (lStory == null)
        //        return null;

        //   var lCategory = lStory.ToList();
        //    var listCategory = new List<SelectListItem>();
        //    listCategory.AddRange(lCategory.Select(item => new SelectListItem { Text = item.Name, Value = item.Id.ToString() }));
        //    Session["Category"] = listCategory;

        //    return Json(listCategory);//, JsonRequestBehavior.AllowGet
        //}

      
        //[HttpGet]
        //public ActionResult Add()
        //{
        //    var categoryType = new CategoryTypeModels();
        //    Type();
        //    Category();

        //    var lType = (List<RelaxTypeDataContract>)_cache["Type"];
        //    if (lType == null)
        //        return RedirectToAction("Index", "Story");
        //    var first = lType.FirstOrDefault();
        //    if (first == null)
        //        return RedirectToAction("Index", "Story");

        //    var lStory = (List<RelaxCategoryDataContract>)_cache["Category"];
        //    categoryType.RelaxCategory = lStory.ToList();
          
        //    LoadLinkType();
        //    categoryType.RelaxType = lType;

        //    return View(categoryType);
        //}
        //[HttpPost]
        //[ValidateInput(false)]
        //public ActionResult Add(FormCollection collection)
        //{
        //    var title = collection["txtName"];
        //    var keyword = collection["txtKeyword"];
        //    var decription = collection["txtDecription"];
        //    var postedfile = Request.Files["fufile"];
        //    if (postedfile != null)
        //    {
        //        var fileName = postedfile.FileName;
        //        if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(keyword) || string.IsNullOrEmpty(decription) ||
        //            string.IsNullOrEmpty(fileName))
        //        {
        //            Response.Redirect("<script>alert('Các trường tiêu đề, hình ảnh, từ khóa, mô tả không được trống.');</script>");
        //            return View();
        //        }
        //        var story = new RelaxStoryDataContract
        //        {
                    
        //        };

        //    }
        //    return View();
        //}

        //[HttpGet]
        //public ActionResult Edit(int id = 0)
        //{
        //    var lStory = (List<RelaxStoryDataContract>)_cache["Story"];
        //    if (lStory == null)
        //        return RedirectToAction("Index","Story");
        //    var first = lStory.FirstOrDefault(p => p.Id == id);
        //    return View(first);
        //}
        //[HttpPost]
        //public ActionResult Edit(FormCollection collection)
        //{
        //    return View();
        //}

        //[HttpGet]
        //public ActionResult Detail(int id = 0)
        //{
        //    var lStory = (List<RelaxStoryDataContract>)_cache["Story"];
        //    if (lStory == null)
        //        return RedirectToAction("Index", "Story");
        //    var first = lStory.FirstOrDefault(p => p.Id == id);
        //    return View(first);
        //}

        //[HttpGet]
        //public ActionResult Delete(long id)
        //{
        //    try
        //    {
        //        //var lId = new List<long> { id };
        //        //var ok = _service1.DeleteStory(lId,Convert.ToInt32(Session["LanguageCode"]));
        //        //if (ok)
        //        //{
        //        //    var lData = (List<RelaxStoryDataContract>)_cache["Story"];
        //        //    var first = lData.FirstOrDefault(p => p.Id == id);
        //        //    if (first != null)
        //        //    {
        //        //        _utility.DeleteFile(Server.MapPath("~/" + first.Image));
        //        //        lData.Remove(first);
        //        //    }

        //        //    _cache["Staff"] = lData;
        //        //}
        //        return RedirectToAction("Index", "Story");
        //    }
        //    catch (Exception ex)
        //    {
        //        Response.Write("<script>alert('chi tiết lỗi:" + ex.Message + "');</script>");
        //        return RedirectToAction("Index", "Story");
        //    }

        //}

        //[HttpGet]
        //public ActionResult PublicImage()
        //{
        //    return View();
        //}
        //[HttpPost]
        //public ActionResult PublicImage(FormCollection collection)
        //{
        //    return View();
        //}

        //[HttpGet]
        //public ActionResult PublicImageAdd()
        //{
        //    return View();
        //}
        //[HttpPost]
        //public ActionResult PublicImageAdd(FormCollection collection)
        //{
        //    return View();
        //}
        //[HttpGet]
        //public ActionResult PublicImageEdit()
        //{
        //    return View();
        //}
        //[HttpPost]
        //public ActionResult PublicImageEdit(FormCollection collection)
        //{
        //    return View();
        //}

        //[HttpGet]
        //public ActionResult PublicStory()
        //{
        //    return View();
        //}
        //[HttpPost]
        //public ActionResult PublicStory(FormCollection collection)
        //{
        //    return View();
        //}
        //[HttpGet]
        //public ActionResult PublicStoryAdd()
        //{
        //    return View();
        //}
        //[HttpPost]
        //public ActionResult PublicStoryAdd(FormCollection collection)
        //{
        //    return View();
        //}
        //[HttpGet]
        //public ActionResult PublicStoryEdit()
        //{
        //    return View();
        //}
        //[HttpPost]
        //public ActionResult PublicStoryEdit(FormCollection collection)
        //{
        //    return View();
        //}
        //[HttpGet]
        //public ActionResult PublicNews()
        //{
        //    return View();
        //}
        //[HttpPost]
        //public ActionResult PublicNews(FormCollection collection)
        //{
        //    return View();
        //}

        //[HttpGet]
        //public ActionResult PublicNewsAdd()
        //{
        //    return View();
        //}
        //[HttpPost]
        //public ActionResult PublicNewsAdd(FormCollection collection)
        //{
        //    return View();
        //}
        //[HttpGet]
        //public ActionResult PublicNewsEdit()
        //{
        //    return View();
        //}
        //[HttpPost]
        //public ActionResult PublicNewsEdit(FormCollection collection)
        //{
        //    return View();
        //}
    }

   
}
